<template>
  <div class="container">
      <!-- -webkit-overflow-scrolling:touch;一般是用于解决IOS滑动卡顿问题，但有时会导致页面卡死滑不动 -->
    <div style="width:100%;height:100%;overflow-x: hidden;overflow-y:auto;">
        <div class="bgBanner">
            <img :src="detail.bannerImg || ''" style="width:100%" alt="">
            <!-- <img src="static/image/prize/prize_card_banner.png" alt=""> -->
        </div>
        <div class="title"  v-if="detail">
            <!--<div class="title_div" v-if="detail"></div>-->
            <p class="title_p">{{detail.rewardTitle}}</p>
        </div>
        <div class="centent">
            <div class="prize_box" v-for="(item, index) in data" :key="index" @click="isLottery(index)" :class="{flipped:num == index}">
                <div class="sub_1">
                    <p>恭喜你！</p>
                    <img :src="prizeItem.rewadImg" alt="">
                    <p>{{prizeItem.itemName}}</p>
                </div>
                <div class="sub_2">
                    <img :src="item" alt="">
                </div>
            </div>
        </div>
        <div class="rule" v-if="detail">
          <p style="display: inline-block;" v-if="detail.limitType === 1">每位用户可参与{{detail.rewardNumLimit}}次</p>
          <p style="display: inline-block;" v-else>每天可翻牌{{detail.rewardNumLimit}}次</p>
          <div @click="model=true;model_rewardDesc=true;">
            <i class="icon njfont nj-yuyue"></i>
            <p style="display: inline-block;">活动规则</p>
          </div>
        </div>
    </div>
    
    <div class="rule_model"  v-if="model" @touchstart="stopEvent" >
    </div>
    <div class="rule_model_1" v-if="model_rewardDesc && !model_span" @click="goOnPrize">
        <article v-html="detail.rewardDesc" style="max-height:15rem;overflow:auto">
        </article>
        <div class="closeReward">我知道了</div>
    </div>
    <div class="rule_prize_model_2" v-if="model_span">
        <div  @touchstart="stopEvent">
            <span class="model_span_1">{{prizeItem.itemName}}</span>
            <img  :src="prizeItem.rewadImg" alt="">
            <span class="model_span_2" >您还剩{{remainRewardAccount-1}}次抽奖机会</span>
        </div>
        <div v-if="remainRewardAccount > 1" class="model_div">
            <span @click="goOnPrize">返回</span>
            <span @click="goOnPrize">继续抽奖</span>
        </div>
        <span class="model_span_4" v-else @click="goOnPrize">确定</span>
    </div>
  </div>
</template>

<script>
    import {
      getSpecialRewardRuleAndDetailForHome,
      getSpecialRewardResultDetailForHome,
      getRandomRewardItem
    } from '../../api/wxh/getData';
    import {Toast} from '../../utils/toast.js';
    import { MessageBox } from '../../ui-components';
    import { CHANGE_NAVIGATION,NAVIGATE } from '../../config/postMessage-types';
    import { mapGetters } from 'vuex';
    export default {
        name: "prize_card",
        data() {
          return {
            num: null,
            rewardModel: '',
            // rid: '',
            flag: true,
            model: false,
            model_span: false,
            model_rewardDesc: false,
            detail: '',
            prizeItem: {
              rewadImg:'',
              itemName:'',
            },
            remainRewardAccount: '',
            toPrize:false,
            data: ['static/image/prize/1.jpg',
            'static/image/prize/2.jpg',
            'static/image/prize/3.jpg',
            'static/image/prize/4.jpg',
            'static/image/prize/5.jpg',
            'static/image/prize/6.jpg'],
            flag_1: false
            // ruleData: ''
          }
        }
        ,
        beforeRouteEnter (to, from, next) {
          if(from.name == "integral"){
            localStorage.setItem("rewardModel", 2);
          } else{
            localStorage.setItem("rewardModel", 3);
          }
          next();
        },

        created(){
            let titleConfig = JSON.stringify({
                type: CHANGE_NAVIGATION,
                params: {
                    title: "翻牌抽奖"
                }
            })
            setTimeout(() => {
                window.postMessage(titleConfig)
            },300)
            this.getData();
        },
        computed: {
            ...mapGetters('wxh', [
                'userInfo'
            ]),
            hasUserInfo() {
                return Object.keys(this.userInfo).length;
            },
        },
        watch: {
            hasUserInfo(newVal,oldVal) {
                if(newVal){
                    this.getData();
                }
            },
        },
        methods:{
            //阻止浏览器默认行为
            stopEvent(event){
                event = event || window.event; 
                if (event.preventDefault) {  // W3C variant 
                    event.preventDefault();
                } else { // IE<9 variant:
                    event.returnValue = false;
                }
                //event.preventDefault?event.preventDefault():(event.returnValue = false);
            },
          async getData(){
            let val = localStorage.getItem("rewardModel");
            let flag = this.isLogin();
            if(!flag) return;
            if(!val)return;

            let req = await getSpecialRewardRuleAndDetailForHome("rewardModel="+val);
            console.log('req1', req.status);
            if(req.status !== 200){
              Toast({
                message: "网络错误",
                type: "fail",
                duration: api.alertTime_2
              })
              return
            }
            if(req.data.returnCode === 0){
              Toast({
                message:  req.data.msg || "抱歉，抽奖活动已结束！",
                type: "fail",
                duration: api.alertTime_2
              })
              return
            }
           
            if(req.data.returnCode == 1){
              if(!req.data.data[0]){
                Toast({
                  message:  "抱歉，抽奖活动已结束！",
                  type: "fail",
                  duration: api.alertTime_2
                })
                return
              }
              this.detail = req.data.data[0];
              this.flag_1 = true;
            }
          },
          //The rest number of lottery;
          async isLottery(index){
            if(!this.flag_1)return;
            let flag = this.isLogin();
            if(!flag) return;
            console.log(this.detail);
            if(!this.detail){
             return MessageBox.alert('抱歉，抽奖活动已结束！')
                .then(action => {
                  // this.$router.push({path: 'login',query: {page: 1}});
                })
                .catch(error => {
                  return error;
                })

                // return
            }

            this.flag_1 = false;
            let res = await  getSpecialRewardResultDetailForHome("rewardId=" + this.detail.rid);
            if(res.data.returnCode == 1){             
              if(res.data.data[0].remainRewardAccount !== 0){               
                this.remainRewardAccount = res.data.data[0].remainRewardAccount;
                this.getPrize();
                this.num = index;
                return
              }
              if(res.data.data[0].remainRewardAccount === 0) {
                Toast({
                  message: res.data.data[0].remainRewardMsg || "还剩0次抽奖机会！",
                  type: "warn",
                  duration: api.alertTime_2
                })
                this.flag_1 = true;
                return
              }
            } else if(res.data.returnCode == 0) {
              Toast({
                message: res.data.msg,
                type: "fail",
                duration: api.alertTime_2
              })
              this.flag_1 = true;
              return
            } else if(!res.data.data[0]) {
               Toast({
                message: "无抽奖数据！",
                type: "fail",
                duration: api.alertTime_2
              })
              this.flag_1 = true;
              return
            }
          },
          //get prize;
          async getPrize(){
            let res = await getRandomRewardItem("rewardId="+this.detail.rid+"&signDetailId=");
              if(res.data[0] && res.returnCode == 1) {
                this.prizeItem = res.data[0];
                setTimeout(()=>{
                  this.model = true
                  this.model_span = true;
                  this.flag_1 = true;
                },800)
                return
              } else {
                Toast({
                  message: "无数据",
                  type: "fail",
                  duration: api.alertTime_2
                })
                this.flag_1 = true;
                return
              }
          }
          ,
          //判断登录回传Rn
          isLogin(){
                let userInfo = this.hasUserInfo;
                let flag = true;
                console.log(window.api.sessionId,'window.api.sessionId')
                if(!userInfo){
                    let goBackIns = {
                        type: NAVIGATE,
                        params: {
                            screen: 'Login',
                            params:{
                                FromWeb: true
                            }
                        }
                    }
                    window.postMessage(JSON.stringify(goBackIns));
                    flag = false;
                }
                console.log('判断登录')
                return flag;
          },
          goOnPrize(){
            //继续抽奖
            this.num = null;
            this.prizeItem = {
              rewadImg:'',
              itemName:'',
            }; 
            this.model=false;
            this.model_span=false;
            this.model_rewardDesc=false;
          },
        }
    }
</script>

<style scoped>
  .container {
    background: #fff;
    height: 100%;
    overflow: hidden;
  }
  .centent {
    width: 100%;
    height: auto;
    /*background: url("../../../static/image/prize/prize_card_bg.png");*/
    background-size: 100% 100%;
    display: flex;
    justify-content: space-around;
    flex-wrap: wrap;
    /*align-items: center;*/
    padding: .5rem 1rem 0rem 1rem;
    background-color: #fff;
    perspective: 1000;
    -moz-perspective: 1000;
    -webkit-perspective: 1000;
  }
  .bgBanner{
      width: 100%;
      overflow-x: hidden;
  }
  .title {
    background: #fff;
    height: 2rem;
    margin-top: 1rem;
    position: relative;
    width: 100%;
  }
  .title_p {
    line-height: 2rem;
    font-size: .7rem !important;
    background: #fff;
    padding-left: 10px;
    padding-right: 10px;
    margin: auto;
    display: flex;
    flex: 0 1 auto;
    flex-direction: row;
    justify-content: center;
    align-items: center;
  }
  .title_p:before, .title_p:after {
      content: '';                 
      background: #494949;       
      width: 17%;
      height: 1px;
  }
  .title_p:before{
      margin-right: 5px;
  }
  .prize_box {
    width: 30%;
    height: auto;
    height: 8rem;
    position: relative;
    flex: 0 1 auto;
    margin-bottom: 3rem;
    transform-style: preserve-3d;
    -webkit-transform-style: preserve-3d;
    transition: 0.5s;
    -webkit-transition: 0.5s;
  }
  .title_p:after {
      margin-left: 5px;
  }
  
  .sub_1, .sub_2{
    width: 100%;
    height: 100%;
    position: absolute;
    backface-visibility: hidden;
    -webkit-backface-visibility: hidden ;
  }

  .sub_1 {
    background: #fff;
    transform: rotateY(180deg);
    -webkit-transform: rotateY(180deg);
  }
  .flipped {
    transform: rotateY(180deg);
    -webkit-transform: rotateY(180deg);
  }
  .rule_model {
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.6);
    position: absolute;
    z-index: 100;
    top: 0;
    left: 0;
    color: #000;
  }
  .rule_model_1 {
    width: 80%;
    height: auto;
    max-height: 80%;
    margin: auto;
    background: rgba(255, 255, 255, 1);
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate3d(-50%, -50%, 0);
    transition: .2s;
    border-radius: 10px;
    overflow-y: scroll;
    overflow-x: hidden;
    z-index: 103;
    /*white-space: nowrap ;*/
    word-break: break-all;
    min-height: 25%;
    text-align: left;
    padding: 1rem 1rem 2rem;
  }
  .rule_prize_model_2 {
    width: 70%;
    height: 35%;
    background: url('../../../static/image/prize/prize_01.png');
    background-repeat: no-repeat;
    background-size: 100% 100%;
    position: absolute;
    top: 50%;
    left: 50%;
    z-index: 103;
    transform: translate3d(-50%, -50%, 0);
    transition: .2s;
    display: flex;
    flex-direction: column;
    justify-content: center;
    padding: 1rem 0;
  }

  .rule_prize_model_2 span {
    display: block;
    height: 1.5rem;
  }
  .rule_prize_model_2 img {
    width: 35%;
    /* height:1rem; */
    /* height: 40%; */
    margin: 0 auto;
  }
  .model_span_1 {
    font-weight: 500;
    font-size:18px;
    color: #fc2e2e;
  }
  .model_span_2 {
    color: #fc2e2e;
  }
  .model_span_3 {
    color: #a29e9e;
    line-height: 1.5rem;
  }
  .model_span_4 {
    color: #191818;
    line-height: 1.5rem;
    /* margin-bottom: 1rem; */
    width: 50%;
    height: 1.5rem;
    text-align: center;
    display: inline-block;
    margin: auto;
    border: 1px solid #9b9c3e;
    border-radius: 5px;
    /* margin-bottom: 1rem; */
  }
  .article {
    position: absolute;
    top: 50%;
    transform: translateY(-50%)
  }
  .model_div {
    width: 100%;
    height: 1.5rem;
    /*display: flex;*/
    /* margin-bottom: 1rem; */
  }
  .model_div span {
    display: inline-block;
    width: 40%;
    height: 100%;
    text-align: center;
    border:1px solid #9b9c3e;
    border-radius: 5px;
    line-height: 1.5rem;
  }
  .rule {
    background: #fff;
    color: #666;
    width: 100%;
    height: 2rem;
    padding-left: 1rem;
    padding-right: 1rem;
    padding-right: 1rem;
    line-height: 2rem;
    text-align: left;
    display: flex;
    justify-content: space-between;
  }
  .closeReward{
    text-align: center;
    border-top: 1px solid #ddd;
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 2rem;
    line-height: 2rem;
    color:#9a9a9a;
    background-color: white;
  }
</style>
